package com.example.testpong;

import java.util.Map;
import java.util.TreeMap;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	Map<String,Integer> scores;
	private SoundPool sounds;
	private int intSon1;
	private int intSonRemove1;
	private int intSonDog;
	private int intSonCat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scores = new TreeMap<String,Integer>();
        scores.put("Yoann", 50);
        scores.put("Jimmy", 100);
        scores.put("Linda", 250);
        scores.put("Vincent", 5000);
        scores.put("BigBossOfTheDead", 99999999);
        //son
        sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
        intSon1 = sounds.load(getBaseContext(), R.raw.click1, 1);
        intSonRemove1 = sounds.load(getBaseContext(), R.raw.remove1, 1);
        intSonDog= sounds.load(getBaseContext(), R.raw.dog, 1);
        intSonCat= sounds.load(getBaseContext(), R.raw.cat, 1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void bark(View view){
    	sounds.play(intSonDog, 1.0f, 1.0f, 0, 0, 1.5f);
    }
    
    public void meow(View view){
    	sounds.play(intSonCat, 1.0f, 1.0f, 0, 0, 1.5f);
    }
    
    
    public void initList(View view){
    	SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    	SharedPreferences.Editor editor = preferences.edit();
    	
    	for (String s : scores.keySet()){
    		editor.putInt(s, scores.get(s));
    	}
    	editor.commit();
    	//son
    	sounds.play(intSon1, 1.0f, 1.0f, 0, 0, 1.5f);
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.classement:
                affClassement();
                return true;
            case R.id.infos:
                return true;
            case R.id.parametres:
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public void viderClassement(){
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    	Map<String, ?> map = prefs.getAll();
    	SharedPreferences.Editor editor = prefs.edit();
    	
    	 for (String s : map.keySet()){
     		editor.remove(s);
         }
    	 editor.commit();
    	 sounds.play(intSonRemove1, 1.0f, 1.0f, 0, 0, 1.5f);
    }
    
    public void affClassement() {
      
    	
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Classement");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Map<String, ?> map = prefs.getAll();
        
        if (map.keySet().size() != 0) {
	        String[] stringArray = new String[scores.size()];
	        //verification
	        /*
	        String tmp = "";
	        for (String s1 : map.keySet())
	        	tmp += s1+" : "+map.get(s1)+"\n";
	        Toast toast = Toast.makeText(this, tmp, Toast.LENGTH_LONG);
	        toast.show();
	        */
	       
	        int i = 0;
	        for (String s : map.keySet()){
	    		stringArray[i] = s +" : "+prefs.getInt(s,-1);
	    		i++;
	        }
	        
	        ListView modeList = new ListView(this);
	        ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
	        modeList.setAdapter(modeAdapter);
	
	        builder.setView(modeList);
	        
	        builder.setNegativeButton("Vider", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    viderClassement();
                }
            });
        
        }else{
        	builder.setMessage("Aucun Classement disponible");
        	
        }
        
        builder.setPositiveButton("Fermer", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.cancel();
                   }
             	});
        
     

        Dialog Dreturn = builder.create();
        Dreturn.show();
    }
	
}
